package com.jike.noobmoney.mvp.view.fragment;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MoneyEntity;
import com.jike.noobmoney.helper.ImageLoader;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.mvp.view.activity.AboutUsActivity;
import com.jike.noobmoney.mvp.view.activity.ChongZhiActivity;
import com.jike.noobmoney.mvp.view.activity.CooperationActivity;
import com.jike.noobmoney.mvp.view.activity.EditPasswordActivity;
import com.jike.noobmoney.mvp.view.activity.ExchangeActivity;
import com.jike.noobmoney.mvp.view.activity.LianXiUsActivity;
import com.jike.noobmoney.mvp.view.activity.MainActivity;
import com.jike.noobmoney.mvp.view.activity.MyInfoActivity;
import com.jike.noobmoney.mvp.view.activity.MyInfoActivityUpdate;
import com.jike.noobmoney.mvp.view.activity.MyPushActivity;
import com.jike.noobmoney.mvp.view.activity.MyTaskActivity;
import com.jike.noobmoney.mvp.view.activity.NewTeachActivity;
import com.jike.noobmoney.mvp.view.activity.PushTaskActivity;
import com.jike.noobmoney.mvp.view.activity.PushTaskActivityNew;
import com.jike.noobmoney.mvp.view.activity.RankActivity;
import com.jike.noobmoney.mvp.view.activity.SettingActivity;
import com.jike.noobmoney.mvp.view.activity.ShouZhiActivity;
import com.jike.noobmoney.mvp.view.activity.TaskShouZhiActivity;
import com.jike.noobmoney.mvp.view.activity.TastListActivity;
import com.jike.noobmoney.mvp.view.activity.TiXianActivity;
import com.jike.noobmoney.mvp.view.activity.TiXianCenterActivity;
import com.jike.noobmoney.mvp.view.activity.TiXianCenterActivityUpdate;
import com.jike.noobmoney.mvp.view.activity.UserXYActivity;
import com.jike.noobmoney.mvp.view.activity.VideoTwoActivity;
import com.jike.noobmoney.mvp.view.activity.YSActivity;
import com.jike.noobmoney.mvp.view.widget.CheckableTextView;
import com.jike.noobmoney.mvp.view.widget.CommonDialog;
import com.jike.noobmoney.mvp.view.widget.TiXianDialog;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.rxbus.RxBus;
import com.jike.noobmoney.rxbus.RxBusRoute;
import com.jike.noobmoney.rxbus.RxEvent;
import com.jike.noobmoney.util.SPUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kekex on 2018/8/21.
 * Home
 */
public class MyFragmentUpdate extends BaseFragment implements IView {


    private UserPresenter userPresenter;
    @BindView(R.id.iv_user_photo)
    ImageView ivUserPhoto;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_user_id)
    TextView tvUserId;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_rwmoney)
    TextView tv_rwmoney;

    @BindView(R.id.tv_clear)
    TextView tv_clear;

    @Override
    protected int layoutResID() {
        return R.layout.fragment_my_update;
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshData();
        tv_clear.setText("清理缓存");
    }


    private void refreshData() {
        if (userPresenter != null) {
            showProgressDialog();
            String userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
            userPresenter.moneyView(userid, ConstantValue.RequestKey.getMoneyView);
        }
    }


    @Override
    protected void initData() {
        userPresenter = new UserPresenter(this);
        //修改个人信息更新
        initView();
    }

    @OnClick({R.id.update_ll,R.id.ll_user, R.id.rl_new_shouzhi, R.id.rl_my_task,R.id.rl_fb,
            R.id.tv_login_out,R.id.rl_new_shouzhi_other,
            R.id.tv_new_chongzhi,R.id.rw_tixian, R.id.tv_new_tixian,R.id.rl_about_us,R.id.rl_cooperation,R.id.rl_my_push,R.id.iv_my_invite, R.id.iv_my_newteach,R.id.yszc,R.id.yhxy
    ,R.id.clear_ll,R.id.img_yq,R.id.rk_id})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_user:
                //我的资料
                startActivity(new Intent(context, MyInfoActivityUpdate.class));
                //startActivity(new Intent(context, TastListActivity.class));
                break;
            case R.id.rk_id:
                //活动入口
                startActivity(new Intent(context, ExchangeActivity.class));
                //startActivity(new Intent(context, TastListActivity.class));
                break;
            case R.id.clear_ll:
                //清理缓存
                        /**
                         *要执行的操作
                         */
                        try {
                //用延时3秒操作来模拟网络操作
                Thread.sleep( 3000 );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//                        Glide.get(getActivity()).clearDiskCache();
//                        Glide.get(getActivity()).clearMemory();
                        tv_clear.setText("已清理完成");


                break;
            case R.id.iv_my_invite:
                MainActivity activity1 = (MainActivity) getActivity();
                if (activity1 != null) {
                    activity1.goRecommend(2);
                }
                break;
            case R.id.iv_my_newteach:
                //新手教程
                Intent intent = new Intent(context, NewTeachActivity.class);
                intent.putExtra("isTask", false);
                startActivity(intent);
                break;
            case R.id.rl_new_shouzhi:
                //收支明细
                ShouZhiActivity.startAction(context, 0);
                break;
            case R.id.update_ll:
                //修改密码
                startActivity(new Intent(context, EditPasswordActivity.class));
                break;

            case R.id.rl_fb:
                //发布悬赏
                startActivity(new Intent(context, PushTaskActivityNew.class));
                break;
            case R.id.rl_new_shouzhi_other:
                //收支明细
                TaskShouZhiActivity.startAction(context, 0);
                break;
            case R.id.tv_new_chongzhi:
                //充值中心
                startActivity(new Intent(context, ChongZhiActivity.class));
                break;
            case R.id.tv_new_tixian:
                //提现中心ok
                startActivity(new Intent(context, TiXianCenterActivityUpdate.class));
                break;
            case R.id.img_yq:
                //邀请
                recommend();
                break;
            case R.id.rw_tixian:
                //提现中心ok
                startActivity(new Intent(context, TiXianCenterActivityUpdate.class).putExtra("type",1));
                break;
            case R.id.rl_my_task:
                //悬赏管理
                startActivity(new Intent(context, MyPushActivity.class));
                break;
            case R.id.rl_my_push:
                //我的悬赏
                startActivity(new Intent(context, MyTaskActivity.class));
                break;
            case R.id.tv_login_out:
                //退出登录
                loginOut();
                break;
            case R.id.rl_about_us:
                //关于我们
                startActivity(new Intent(context, AboutUsActivity.class));
                break;
            case R.id.rl_cooperation:
                //商务合作
                startActivity(new Intent(context, CooperationActivity.class));
                break;
            case R.id.yhxy:
                //用户协议
                startActivity(new Intent(context, UserXYActivity.class));
                break;
            case R.id.yszc:
                //隐私协议
                startActivity(new Intent(context, YSActivity.class));
                break;
        }
    }

    /**
     * 提现
     */
    private void tixian() {
        TiXianDialog bottomDialog = TiXianDialog.getInstance();
        bottomDialog.show(getFragmentManager(), "tixian");
        bottomDialog.setClickCallback(() -> {
            Intent intent = new Intent(context, TiXianActivity.class);
            startActivityForResult(intent, 100);
            bottomDialog.dismiss();
        });
    }
    /**
     * 点击分享
     */
    public void recommend() {
        String recode = SPUtils.getInstance().getString(ConstantValue.SpType.recode); //分享
        String taskurl = SPUtils.getInstance().getString(ConstantValue.SpType.taskurl);
        String url = !TextUtils.isEmpty(taskurl) ? taskurl + "?recode=" + recode : ConstantValue.COMMENTURLCOPY + recode;
        UMWeb web = new UMWeb(url);
        web.setTitle("小白赚钱");
        web.setThumb(new UMImage(context, R.mipmap.ic_launcher));
        web.setDescription(getString(R.string.text_yaoqing1));
        ShareAction shareAction = new ShareAction(getActivity());
        shareAction.withMedia(web);
        shareAction.setDisplayList(SHARE_MEDIA.QZONE, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE);
        shareAction.setCallback(shareListener);
        shareAction.open();
    }


    private UMShareListener shareListener = new UMShareListener() {

        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            //ToastUtils.showShortToastSafe("分享成功了");
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            //ToastUtils.showShortToastSafe("分享失败了");

        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            //ToastUtils.showShortToastSafe("取消了");
        }
    };


    /**
     * 退出登录
     */
    private void loginOut() {
        CommonDialog loginOutDialog = CommonDialog.getInstance();
        loginOutDialog.show(getFragmentManager(), "loginout");
        loginOutDialog.setClickCallback(new CommonDialog.OnClickCallback() {
            @Override
            public void cancel() {
                loginOutDialog.dismiss();
            }

            @Override
            public void confirm() {
                RxBus.getInstance().post(new RxEvent(RxBusRoute.LOGIN_OUT));
                loginOutDialog.dismiss();
            }
        });
    }

    @Override
    protected boolean isRegistRxBus() {
        return true;
    }

    /**
     * 注册订阅事件
     */
    protected void receiveEvent(RxEvent rxEvent) {
        switch (rxEvent.busName) {
            case RxBusRoute.REFRESH_USER_INFO:
                initView();
                break;
        }
    }

    private void initView() {
        String nick = SPUtils.getInstance().getString(ConstantValue.SpType.nick);
        String userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        //String avatar = SPUtils.getInstance().getString(ConstantValue.SpType.avatar);
        String newavatar = SPUtils.getInstance().getString(ConstantValue.SpType.newavatar);


        if (TextUtils.isEmpty(newavatar)) {
            if (ivUserPhoto != null) {
                ImageLoader.displayImageDefaultHead(context, R.drawable.ic_user_default, ivUserPhoto);
            }
        } else {
            if (ivUserPhoto != null) {
                String url = ConstantValue.TX_IMG_BASEURL + newavatar;
                ImageLoader.displayImageDefaultHead(context, url, ivUserPhoto);
            }
        }

        //昵称
        if (tvUserName != null) {
            tvUserName.setText(TextUtils.isEmpty(nick) ? "点击设置昵称" : nick);
        }

        //用户id
        if (tvUserId != null) {
            tvUserId.setText("ID:" + userid);
        }

    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgressDialog();
        if (ConstantValue.RequestKey.getMoneyView.equals(actionType)) {
            //获取用户余额
            MoneyEntity.UserBean userBean = (MoneyEntity.UserBean) data;
            tvMoney.setText("" + userBean.getMoney());
            tv_rwmoney.setText(""+userBean.getRwmoney());
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        dismissProgressDialog();
    }
}
